package net.ib.mtalk.ui.widget;

import net.ib.mtalk.R;
import net.ib.mtalk.util.MathUtils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TabHost;

/**
 * 화면 드레그터치시에 화면 전환 을 위한 TabHost
 * {@link TabHost}
 * 
 * @use XML 사용시 <TabHost></TabHost> 대신 <view class="net.ib.mtalk.ui.widget.FlingableTabHost"></view> 사용.
 * @author happy2v
 *
 */
public class FlingableTabHost extends TabHost {
	GestureDetector mGestureDetector;

    Animation mRightInAnimation;
    Animation mRightOutAnimation;
    Animation mLeftInAnimation;
    Animation mLeftOutAnimation;

    public FlingableTabHost(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRightInAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
        mRightOutAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
        mLeftInAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
        mLeftOutAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_left_out);

        final int minScaledFlingVelocity = ViewConfiguration.get(context)
                .getScaledMinimumFlingVelocity(); // 10 = fudge by experimentation

        mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                int tabCount = getTabWidget().getTabCount();
                int currentTab = getCurrentTab();
                if (Math.abs(velocityX) > minScaledFlingVelocity * 4 &&
                    Math.abs(velocityY) < minScaledFlingVelocity * 10) {

                    final boolean right = velocityX < 0;
                    final int newTab = MathUtils.constrain(currentTab + (right ? 1 : -1),
                            0, tabCount - 1);
                    if (newTab != currentTab) {
                        // Somewhat hacky, depends on current implementation of TabHost:
                        // http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;
                        // f=core/java/android/widget/TabHost.java
                        View currentView = getCurrentView();
                        setCurrentTab(newTab);
                        View newView = getCurrentView();

                        newView.startAnimation(right ? mRightInAnimation : mLeftInAnimation);
                        currentView.startAnimation(
                                right ? mRightOutAnimation : mLeftOutAnimation);
                    }
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });
    }

    
    @Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
    	if (mGestureDetector.onTouchEvent(ev)) {
            return true;
        }
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mGestureDetector.onTouchEvent(event)) {
            return true;
        }
		return super.onTouchEvent(event);
	}

	@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (mGestureDetector.onTouchEvent(ev)) {
            return true;
        }
        return super.onInterceptTouchEvent(ev);
    }
}